import {
  addRules,
  updateRules,
  importData,
  getZipCodeByStateBatch,
  deleteRules,
  exportData,
  fetchRules,
  addProviderInfo,
  downloadAttachment,
  deleteProviderInfo,
  getAllServiceProvider,
  fetchRulesList,
  getAllStateList
} from '../services/API';
import { message } from 'antd';
export default {
  namespace: 'returnRoules',

  state: {
    rouleId: '',
    rulesList: [], //规则列表
    getAllStateList: [],
    stepCurrent: 0,
    rulesDetail: null, //规则详情
    showDrawer: false,
    activeKey: 0, //激活的折叠面板
    serviceProvidersList: [], //服务信息列表
    getZipCodeData: [], //邮编
    attachmentUrl: ''
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const res = yield call(fetchRulesList, payload);
      if (res.code === 200) {
        yield put({
          type: 'save',
          payload: {
            rulesList: res.returnRules
          }
        });
      }
    },
    *getAllStateList({ payload }, { call, put }) {
      const res = yield call(getAllStateList, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            getAllStateList: res
          }
        });
      }
    },
    //添加
    *add({ payload, callback }, { call, put }) {
      const res = yield call(addRules, payload);
      if (res.code === 200) {
        // message.success(res.msg || 'Operation succeeded');
        yield put({
          type: 'save',
          payload: {
            rouleId: res.id
          }
        });
        callback && callback();
      }
    },
    // 修改
    *update({ payload, callback }, { call }) {
      const res = yield call(updateRules, payload);
      if (res.code === 200) {
        message.success('Operation succeeded');
        callback && callback();
      }
    },
    // 查询详情
    *fetchRules({ payload, callback }, { call, put }) {
      const res = yield call(fetchRules, payload);
      if (res.code === 200) {
        yield put({
          type: 'save',
          payload: {
            rulesDetail: res.returnRules
          }
        });
      }
      callback && callback(res.returnRules);
    },
    // 添加服务信息
    *addProviderInfo({ payload, callback }, { call, put }) {
      const res = yield call(addProviderInfo, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
        yield put({
          type: 'getAllServiceProvider'
        });
        callback && callback();
      }
    },
    // 删除服务信息
    *deleteProviderInfo({ payload }, { call, put }) {
      const res = yield call(deleteProviderInfo, payload);
      if (res.code === 200) {
        message.success('Delete succeeded');
        yield put({
          type: 'getAllServiceProvider'
        });
      }
    },
    // 获取服务信息
    *getAllServiceProvider({ payload }, { call, put }) {
      const res = yield call(getAllServiceProvider, payload);
      if (res.code === 200) {
        // console.log(res,'数据33')
        yield put({
          type: 'save',
          payload: {
            serviceProvidersList: res.data
          }
        });
      }
    },
    // 获取邮编
    *getZipCodeByStateBatch({ payload }, { call, put }) {
      const res = yield call(getZipCodeByStateBatch, payload);
      if (res && res.length > 0) {
        console.log(res, '邮编');
        let zipCode = [];
        res.forEach(a => {
          zipCode.push(a.zipCode);
        });
        yield put({
          type: 'save',
          payload: {
            getZipCodeData: zipCode
          }
        });
      }
    },
    // 删除规则
    *deleteRules({ payload, callback }, { call, put }) {
      const res = yield call(deleteRules, payload);
      if (res.code === 200) {
        !callback && message.success(res.msg || 'Operation succeeded');
        const userInfo = JSON.parse(localStorage.getItem('userInfo'));
        yield put({
          type: 'fetch',
          payload: {
            shopsId: userInfo.shopId
          }
        });
        if (callback) {
          yield put({
            type: 'save',
            payload: {
              showDrawer: false,
              rulesDetail: null,
              stepCurrent: 0
            }
          });
        }
      }
      callback && callback();
    },
    // 导出产品
    *exportData({ payload }, { call }) {
      const res = yield call(exportData, payload);
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
      }
    },
    // 导入产品
    *importData({ payload }, { call, put }) {
      const res = yield call(importData, payload, {
        showMessage: false
      });
      if (res.code === 200) {
        message.success(res.msg || 'Operation succeeded');
        yield put({
          type: 'save',
          payload: {
            attachmentUrl: res.data
          }
        });
      } else {
        message.error('Import failed, please check the file format');
      }
    },
    // 下载
    *downloadAttachment({ payload }, { call }) {
      yield call(downloadAttachment, payload);
      // const res = yield call(downloadAttachment, payload);
      // if (res.code === 200) {
      //   message.success(res.msg || 'Operation succeeded');
      // }
    }
  },

  reducers: {
    save(state, action) {
      // console.log(action, '更新');
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/returnRoules') {
          const userInfo = JSON.parse(localStorage.getItem('userInfo'));
          if (!userInfo) return;
          dispatch({
            type: 'fetch',
            payload: {
              shopsId: userInfo.shopId
            }
          });
        }
      });
    }
  }
};
